package com.github.postsanf.yinian.service;

import android.content.Intent;
import android.util.Log;

import com.github.postsanf.yinian.BaseIntentService;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.utils.SpUtils;
import com.litesuits.http.exception.HttpException;
import com.litesuits.http.listener.HttpListener;
import com.litesuits.http.request.StringRequest;
import com.litesuits.http.request.param.HttpMethods;
import com.litesuits.http.response.Response;

import java.util.LinkedHashMap;

/**
 * Created by Administrator on 2016/11/6.
 */

public class StatisticsService extends BaseIntentService {

    public StatisticsService() {
        super("StatisticsService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if (intent!=null){
            final String action = intent.getAction();
            switch (action){
                case ACTIONs.uploadRecode:
                    String json = intent.getStringExtra("json");
                    uploadData(json);
                    break;

                //继续上传上次上传失败的数据
                case ACTIONs.failJson:
                    String failJson = intent.getStringExtra("failJson");
                    uploadFiledJson(failJson);
                    break;
            }
        }
    }

    //上传经过记录的json字符串
    private void uploadData(final String json) {
        final LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        mParam.put(CommonConstants.YNS_RESULT, json);
        StringRequest req = new StringRequest("https://api.zhuiyinanian.com/YinianProject/data/YinianDataInterface")
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setMaxRetryTimes(3000)
                .setMaxRedirectTimes(3000)
                .setHttpListener(new HttpListener<String>() {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
//                        showToast("上传成功");
                        super.onSuccess(s, response);
                    }
                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
                        //上传失败则将数据先存起来，下次进入应用时再上传
                        SpUtils.put(StatisticsService.this,"failJson",json);
                        showToast(CommonConstants.YN_NET_LINK);
                        Log.d("yuhao", "onFailure: ");
                    }
                });
        liteHttp.executeAsync(req);
    }

    //继续上传上次上传失败的数据
    private void uploadFiledJson(final String json) {
        final LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        mParam.put(CommonConstants.YNS_RESULT, json);
        StringRequest req = new StringRequest("https://api.zhuiyinanian.com/YinianProject/data/YinianDataInterface")
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setMaxRetryTimes(3000)
                .setMaxRedirectTimes(3000)
                .setHttpListener(new HttpListener<String>() {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
//                        showToast("上传成功_uploadFiledJson");
                        SpUtils.put(StatisticsService.this,"failJson",null);
                        super.onSuccess(s, response);
                    }
                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
//                        showToast(CommonConstants.YN_NET_LINK);
                    }
                });
        liteHttp.executeAsync(req);
    }
}
